//
//  YDRechargeTableCell.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/14.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDRechargeTableCell.h"
#import "YDBankCardModel.h"
#import "UIImageView+WebCache.h"
@interface YDRechargeTableCell()
//背景图片
@property (nonatomic , weak) UIView *bankBack;
//银行图片
@property (nonatomic , weak) UIImageView *bankImage;
//银行名称
@property (nonatomic , weak) UILabel *bankName;
//修改信息
@property (nonatomic , weak) SYNoHeightedButton *changeName;
//账号
@property (nonatomic , weak) UILabel *bankAccount;
//选择按钮
@property (nonatomic , weak) SYNoHeightedButton *selectedButton;

@end

@implementation YDRechargeTableCell

-(void)setModel:(YDBankCardModel *)model
{
    _model = model;
    
    self.selectedButton.selected = model.isSelected;
    //名称
    self.bankName.text = model.bankName;
    //账号
    self.bankAccount.text = model.fourStarFormatterBankCard;
    //图片
    [self.bankImage sd_setImageWithURL:[NSURL URLWithString:model.bankIconLink] placeholderImage:[UIImage imageNamed:@"bankcopy"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image == nil) {
            self.bankBack.backgroundColor = [UIColor colorWithHexString:blueyGrey];
        }else
        {
            if ([model.bankName isEqualToString:@"中国民生银行"]) {//民生取色为灰色
                self.bankBack.backgroundColor = NumberColor(20, 153, 76);
            }else
            {
                self.bankBack.backgroundColor = [UIImage mostColor:image];
            }
        }
        
       
    }];
  
}

-(void)setPayStyle:(PayStyle)payStyle
{
    _payStyle = payStyle;
    switch (payStyle) {
        case Recharge:
            
            break;
        case Withdraw:
            [self changeName];
            break;
        default:
            break;
    }
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *rechargeTableCell = @"YDRechargeTableCell";
    YDRechargeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:rechargeTableCell];
    if (cell == nil) {
        cell = [[YDRechargeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rechargeTableCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = NumberColor(247, 247, 247);
        self.backgroundColor = NumberColor(247, 247, 247);
        [self bankBack];
//        [self noBankImage];
        [self bankImage];
        [self selectedButton];
        [self bankName];
        [self bankAccount];
//        [self changeName];
       
    }
    return self;
}

-(UIView *)bankBack
{
    if (_bankBack == nil) {
        UIView *bankBack = [[UIView alloc] init];
        [self.contentView addSubview:bankBack];
        bankBack.backgroundColor = NumberColor(66, 119, 232);
        bankBack.userInteractionEnabled = YES;
        [bankBack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(rate375(18));
            make.right.equalTo(self).offset(-rate375(18));
            make.top.equalTo(self);
            if (isPhoneX) {
                make.bottom.equalTo(self).offset(-rateX812(5));
            }else{
                make.bottom.equalTo(self).offset(-rate750(5));
            }
        }];
        bankBack.layer.cornerRadius = 5.0;
        bankBack.layer.masksToBounds = YES;
        _bankBack = bankBack;
    }
    return _bankBack;
}

-(UIImageView *)bankImage
{
    if (_bankImage == nil) {
        UIImageView *bankImage = [[UIImageView alloc] init];
        [self.contentView addSubview:bankImage];
        bankImage.image = [UIImage imageWithColor:[UIColor clearColor]];
        bankImage.userInteractionEnabled = YES;
        [bankImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bankBack);
            make.left.equalTo(self.bankBack.mas_left).offset(rate375(16));
            make.height.width.equalTo(@(rate375(60)));
        }];
        bankImage.layer.cornerRadius = rate375(60) * 0.5;
        bankImage.layer.masksToBounds = YES;

        _bankImage = bankImage;
    }
    return _bankImage;
}

-(SYNoHeightedButton *)selectedButton
{
    if (_selectedButton == nil) {
        SYNoHeightedButton *selectedButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:selectedButton];
        [selectedButton setImage:[UIImage imageNamed:@"mine-weixuan"] forState:UIControlStateNormal];
        [selectedButton setImage:[UIImage imageNamed:@"mine-xuanzhong"] forState:UIControlStateSelected];
        [selectedButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchDown];
        [selectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bankBack.mas_right).offset(-rate375(10));
            if (isPhoneX) {
                make.top.equalTo(self.bankBack.mas_top).offset(rateX812(10));
            }else{
                make.top.equalTo(self.bankBack.mas_top).offset(rate750(10));
            }
        }];
        _selectedButton = selectedButton;
    }
    return _selectedButton;
}
//选择按钮监听点击
-(void)selectedButtonClick:(SYNoHeightedButton *)button
{
    
   // button.selected = !button.selected;
    
    if ([self.delegate respondsToSelector:@selector(rechargeTableCellDidSelected:)]) {
        [self.delegate rechargeTableCellDidSelected:self];
    }
}

//银行名称
-(UILabel *)bankName
{
    if (_bankName == nil) {
        UILabel *bankName = [[UILabel alloc] init];
        [self.contentView addSubview:bankName];
        bankName.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
        bankName.textColor = [UIColor whiteColor];
        bankName.textAlignment = NSTextAlignmentLeft;
        bankName.text = @"中国建设银行";
        [bankName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bankImage.mas_right).offset(rate375(8));
            make.bottom.equalTo(self.bankImage.mas_centerY).offset(-rate750(5));
            make.width.equalTo(@(rate375(80)));
        }];
    
        bankName.adjustsFontSizeToFitWidth = YES;
        _bankName = bankName;
    }
    return _bankName;
}

//银行账号
-(UILabel *)bankAccount
{
    if (_bankAccount == nil) {
        UILabel *bankAccount = [[UILabel alloc] init];
        [self.contentView addSubview:bankAccount];
        bankAccount.font = [UIFont boldSystemFontOfSize:font20ScreenWRate * LKScreenW];
        bankAccount.textColor = [UIColor whiteColor];
        bankAccount.textAlignment = NSTextAlignmentLeft;
        bankAccount.text = @"1234 5678 1234 5678 123";
        [bankAccount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bankImage.mas_right).offset(rate375(8));
            make.top.equalTo(self.bankImage.mas_centerY).offset(rate750(1));
            make.right.equalTo(self.bankBack.mas_right).offset(-rate375(5));
        }];
        bankAccount.adjustsFontSizeToFitWidth = YES;
        _bankAccount = bankAccount;
    }
    return _bankAccount;
}


//填写信息按钮
-(SYNoHeightedButton *)changeName
{
    if (_changeName == nil) {
        SYNoHeightedButton *changeName = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:changeName];
        [changeName setBackgroundColor:NumberColor(255,255,255)];
        [changeName setTitle:@"修改支行" forState:UIControlStateNormal];
        changeName.titleLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
        [changeName setTitleColor:[UIColor colorWithHexString:fadedOrange] forState:UIControlStateNormal];
        changeName.titleLabel.textAlignment = NSTextAlignmentCenter;
//        changeName.titleLabel.adjustsFontSizeToFitWidth = YES;

        [changeName addTarget:self action:@selector(changeNameButtonClick:) forControlEvents:UIControlEventTouchDown];
        [changeName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bankName);
            make.left.equalTo(self.bankName.mas_right).offset(rate375(5));

            if (isPhoneX) {
                make.height.equalTo(@(rateX812(25)));
            }else{
                make.height.equalTo(@(rate750(25)));
            }
            make.width.equalTo(@(rate375(68)));
        }];

        changeName.layer.cornerRadius = 3.0;
        changeName.layer.masksToBounds = YES;
//        changeName.layer.borderWidth = 1.0f;
//        changeName.layer.borderColor = [UIColor whiteColor].CGColor;


        _changeName = changeName;
    }
    return _changeName;
}
-(void)changeNameButtonClick:(SYNoHeightedButton *)button
{
    if ([self.delegate respondsToSelector:@selector(rechargeTableCellDidClickChangeBankInformation:)]) {
        [self.delegate rechargeTableCellDidClickChangeBankInformation:self];
    }
}

-(UIImageView *)noBankImage
{
    if (_noBankImage == nil) {
        UIImageView *noBankImage = [[UIImageView alloc] init];
        [self.contentView addSubview:noBankImage];
        noBankImage.image = [UIImage imageNamed:@"mine-wuka"];
        noBankImage.userInteractionEnabled = YES;
        [noBankImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(self.bankBack);
        }];
        noBankImage.layer.cornerRadius = 5.0;
        noBankImage.layer.masksToBounds = YES;
        noBankImage.layer.borderWidth = 1.0;
        noBankImage.layer.borderColor = [UIColor colorWithHexString:paleRed].CGColor;
        _noBankImage = noBankImage;
    }
    return _noBankImage;
}
@end
